package com.taldh.algorithm;

import com.taldh.algorithm.base.ListNode;

public class HasCycle  {
    public boolean hasCycle(ListNode head) {
        if (head == null) return false;

        ListNode fastNode = head;
        ListNode slowNode = head;

        while (fastNode != null && slowNode !=null) {
            if (fastNode.next == null) return false;

            fastNode = fastNode.next.next;
            slowNode = slowNode.next;

            if (fastNode == slowNode) {
                return true;
            }
        }

        return false;
    }
}
